layui.use(['form', 'layer', 'layedit', 'laydate', 'upload'], function () {
    var form = layui.form;
    var layer = parent.layer === undefined ? layui.layer : top.layer;
    var laypage = layui.laypage;
    var upload = layui.upload;
    var layedit = layui.layedit;
    var laydate = layui.laydate;
    var $ = layui.jquery;

    //创建一个编辑器
    var editIndex = layedit.build('news_content', {
        height: 535,
        uploadImage: {
            url: $("#baseURL").val() + "static/json/newsImg.json"
        }
    });

    //用于同步编辑器内容到textarea
    layedit.sync(editIndex);

    //上传缩略图
    upload.render({
        elem: '.thumbBox',
        url: $("#baseURL").val() + "upload/upload",
        accept : "images",
        method: "post",
        done: function (data, index, upload) {
            if (data.code == 1 && data.data != null) {
                $('.thumbImg').attr('src', data.data);
                $('.thumbBox').css("background", "#fff");
            } else {
                layer.msg("文件上传失败");
            }
        }
    });

    //格式化时间
    function filterTime(val) {
        if (val < 10) {
            return "0" + val;
        } else {
            return val;
        }
    }

    //定时发布
    var time = new Date();
    var submitTime = time.getFullYear() + '-' + filterTime(time.getMonth() + 1) + '-' + filterTime(time.getDate()) + ' ' + filterTime(time.getHours()) + ':' + filterTime(time.getMinutes()) + ':' + filterTime(time.getSeconds());
    laydate.render({
        elem: '#release',
        type: 'datetime',
        trigger: "click",
        done: function (value, date, endDate) {
            submitTime = value;
        }
    });

    form.on("radio(release)", function (data) {
        if (data.elem.title == "定时发布") {
            $(".releaseDate").removeClass("layui-hide");
            $(".releaseDate #release").attr("lay-verify", "required");
        } else {
            $(".releaseDate").addClass("layui-hide");
            $(".releaseDate #release").removeAttr("lay-verify");
            submitTime = time.getFullYear() + '-' + (time.getMonth() + 1) + '-' + time.getDate() + ' ' + time.getHours() + ':' + time.getMinutes() + ':' + time.getSeconds();
        }
    });

    form.on("submit(addNews)", function (data) {
        //截取文章内容中的一部分文字放入文章摘要
        var abstract = layedit.getText(editIndex).substring(0, 50);
        $("#news_content").val(layedit.getContent(editIndex));
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.8});
        $.post($("#baseURL").val() + 'news/addOrUpdate',{
            newsName : $(".newsName").val(),  //文章标题
            newsAbstract : $(".newsAbstract").val(),  //文章摘要
            content : layedit.getContent(editIndex).split('<audio controls="controls" style="display: none;"></audio>')[0],  //文章内容
            newsImg : $(".thumbImg").attr("src"),  //缩略图
            category : 1,    //文章分类
            newsStatus : $('.newsStatus select').val(),    //发布状态
            newsTime : submitTime,    //发布时间
            newsTop : $("#newsTop").val() == "on" ? 1 : 0
        },function(res){

        });
        setTimeout(function () {
            top.layer.close(index);
            top.layer.msg("文章添加成功！");
            layer.closeAll("iframe");
            //刷新父页面
            parent.location.reload();
        }, 500);
        return false;
    });

    function cancel() {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    }

    initParamData();

    function initParamData() {
        var id = $("#id").val();
        if(paramIsEmpty(id) || id <= 0) {
            return;
        }

        $.ajax({
            url: $("#baseURL").val() + 'news/find',
            data: {
                "id": id
            },
            dataType: 'json',
            cache: false,
            type: 'post',
            async: false,
            success: function (data) {
                debugger;
                var index = parent.layer.getFrameIndex(window.name);//获取父窗口索引
                var body = parent.layer.getChildFrame('body', index);
                if (data.code == 1 && data.data != null) {
                    var entity = data.data;
                    body.find(".newsName").val(entity.newsName);
                    body.find(".newsAbstract").val(entity.newsAbstract);
                    body.find(".thumbImg").attr("src", entity.newsImg);
                    body.find("#news_content").val(entity.content);
                    body.find(".newsStatus select").val(entity.newsStatus);
                    body.find(".openness input[name='newsLook'][title='" + entity.newsLook + "']").prop("checked", "checked");
                    body.find(".newsTop input[name='newsTop']").prop("checked", entity.newsTop);
                    layedit.setContent(editIndex, entity.content, true);
                    form.render();
                } else {
                    layer.msg('查询文章失败：' + data.msg);
                }
            },
            error: function () {
                layer.msg('系统异常,请联系系统管理员');
            }
        });
    }
});

